package com.xframe.domain.knowledgeBase;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.xframe.domain.BaseModel;
import com.xframe.domain.repository.SysBaseRepository;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.xdal.core.annotation.*;
import com.xframe.xdal.core.constant.AutoFillType;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.ConditionValueCollection;
import com.xframe.xdal.core.model.UpdateAttr;
import com.xframe.xdal.core.model.UpdateWhereMode;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.tran.XTransaction;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.List;

//com.xframe.domain.knowledgeBase.KnowledgeBase
@Data
@Accessors(chain=true)
@XTable(value = "knowledge_base", label = "知识库主表")
@XBelongsTo(typeName = "com.xframe.domain.knowledgeBase.SystemEmployee",foreignKey = "knRepairBy", targetKey = "id",as = "refRepairBy")
@XHasMany(typeName = "com.xframe.domain.knowledgeBase.KnowledgeFiles",foreignKey = "knId", targetKey = "knId",as = "fileList")
public class KnowledgeBase extends BaseModel {
    /**
     * 主键ID
     */
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @AutoFill(AutoFillType.Snowflake)
    @XColumn(name = "kn_id", primaryKeyFlag = true, desc = "主键ID")
    private Long knId;

    /**
     * 单据ID
     */
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "doc_id", desc = "单据ID/经验ID")
    private Long docId;

    /**
     * 知识类型
     */
    @XColumn(name = "kn_type", desc = "知识类型")
    private Integer knType;

    /**
     * 知识编码
     */
    @XColumn(name = "kn_code", uniqueFlag = true, desc = "知识编码")
    private String knCode;

    /**
     * 知识名称
     */
    @XColumn(name = "kn_name", desc = "知识名称")
    private String knName;

    /**
     * 知识来源 值列表KNOWLEDGE_SOURCE
     */
    @XColumn(name = "kn_source", desc = "知识来源")
    private Integer knSource;

    /**
     * 设备ID
     */
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "em_id", desc = "设备ID")
    private Long emId;

    /**
     * 设备名称
     */
    @XColumn(name = "em_name", desc = "设备名称")
    private String emName;

    /**
     * 故障现象 值列表	TYPE_REP_FAULT
     */
    @XColumn(name = "fault_pho", desc = "故障现象")
    private Integer faultPho;

    /**
     * 故障描述
     */
    @XColumn(name = "fault_intro", desc = "故障描述")
    private String faultIntro;

    /**
     * 故障类型 值列表	TYPE_REP_MAINTENANCE
     */
    @XColumn(name = "fault_type", desc = "故障类型")
    private Integer faultType;

    /**
     * 故障原因 值列表	TYPE_REP_REASON
     */
    @XColumn(name = "fault_reason", desc = "故障原因")
    private Integer faultReason;

    /**
     * 解决方法
     */
    @XColumn(name = "fault_solution", desc = "解决方法")
    private String faultSolution;

    /**
     * 维修单编号
     */
    @XColumn(name = "work_order_code", desc = "维修单编号")
    private String workOrderCode;

    /**
     * 知识状态 值列表KNOWLEDGE_STATUS
     */
    @XColumn(name = "kn_status", desc = "知识状态")
    private Integer knStatus;

    /**
     * 上传时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @XColumn(name = "kn_time", desc = "上传时间")
    private Date knTime;

    /**
     * 维修人ID
     */
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "kn_repair_by", desc = "维修人ID")
    private Long knRepairBy;


    /**
     * 维修人信息
     */
    private SystemEmployee refRepairBy;

    //    附件信息
    private List<KnowledgeFiles> fileList;

    //    提交
    @XTransaction
    @XAction(code = "submit", desc = "提交")
    public Integer submit(SysBaseRepository repository, IUserCredentials userCredentials) throws DalFrameException {
        this.knStatus = 2;
        this.setBaseAttrVal(userCredentials);
        UpdateAttr updateAttr = new UpdateAttr(UpdateWhereMode.PK);
        updateAttr.addfieldName("knStatus");
        return repository.update(this, TableInfoBuilder.BuildTableInfoByAnnotation(KnowledgeBase.class), updateAttr);
    }

    //    审核通过
    @XTransaction
    @XAction(code = "pass", desc = "审核通过")
    public Integer pass(SysBaseRepository repository, IUserCredentials userCredentials) throws DalFrameException {
        this.knStatus = 3;
        this.knTime = new Date();
        this.setBaseAttrVal(userCredentials);
        UpdateAttr updateAttr = new UpdateAttr(UpdateWhereMode.PK);
        updateAttr.addfieldName("knStatus");
        updateAttr.addfieldName("knTime");
        return repository.update(this, TableInfoBuilder.BuildTableInfoByAnnotation(KnowledgeBase.class), updateAttr);
    }

    //    审核驳回
    @XTransaction
    @XAction(code = "refuse", desc = "审核驳回")
    public Integer refuse(SysBaseRepository repository, IUserCredentials userCredentials) throws DalFrameException {
        this.knStatus = 4;
        this.setBaseAttrVal(userCredentials);
        UpdateAttr updateAttr = new UpdateAttr(UpdateWhereMode.PK);
        updateAttr.addfieldName("knStatus");
        return repository.update(this, TableInfoBuilder.BuildTableInfoByAnnotation(KnowledgeBase.class), updateAttr);
    }

    //    作废
    @XTransaction
    @XAction(code = "cancel", desc = "作废")
    public Integer cancel(SysBaseRepository repository, IUserCredentials userCredentials) throws DalFrameException {
        this.knStatus = 5;
        this.setBaseAttrVal(userCredentials);
        UpdateAttr updateAttr = new UpdateAttr(UpdateWhereMode.PK);
        updateAttr.addfieldName("knStatus");
        ConditionValueCollection conditionValueCollection = new ConditionValueCollection();
        conditionValueCollection.add(new ConditionValue("knId", this.knId));
        repository.getDataBaseAccess().deleteMany(TableInfoBuilder.BuildTableInfoByAnnotation(KnowledgeComments.class), conditionValueCollection);

        return repository.update(this, TableInfoBuilder.BuildTableInfoByAnnotation(KnowledgeBase.class), updateAttr);
    }
}
